package com.smh;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author shiminghui
 * @date 2025/1/30 11:00
 * @description: TODO
 */
public class _002动态数组 {
    public static void main(String[] args) {
        Array array = new Array(2);
        array.addLast(1);
        array.addLast(2);
        array.add(0, 0);
        array.addLast(3);
        String string = array.toString();
        array.forEach(System.out::println);
        System.out.println(string);
    }

    @Test
    public void test002() {
        Array array = new Array(2);
        array.addLast(1);
        array.addLast(2);
        array.add(0, 0);
        array.addLast(3);
        Iterator<Integer> iterator = array.iterator();
//        while (iterator.hasNext()){
//            System.out.println(iterator.next());
//        }
        for (Integer i : array) { // 只有实现Iterable接口的类才可以使用for each
            System.out.println(i);
        }
    }

    @Test
    public void test003() {
        Array array = new Array(2);
        array.addLast(1);
        array.addLast(2);
        array.add(0, 0);
        array.addLast(3);
        array.stream().forEach(System.out::println);
    }

    @Test
    public void test004() {
        Array array = new Array(2);
        array.addLast(1);
        array.addLast(2);
        array.add(0, 0);
        array.addLast(3);
        assertEquals(0, array.del(0));
        assertIterableEquals(array, Arrays.asList(1, 2, 3));

    }

    private static class Array implements Iterable<Integer> {
        private int[] data;
        private int size;
        // 容量
        private int capacity = 8;


        public Array(int capacity) {
            this.data = new int[capacity];
            this.size = 0;
            this.capacity = capacity;
        }

        public Array() {
            this.data = new int[capacity];
            this.size = 0;
        }

        public void addLast(int value) {
            if (size == capacity) {
                expansion();
            }
            data[size] = value;
            size++;
        }

        public void add(int index, int value) {
            if (index < 0 || index >= size) {
                throw new IllegalArgumentException("index is illegal");
            }
            if (size == capacity) {
                expansion();
            }
//            for (int i = size; i > index; i--) {
//                data[i] = data[i - 1];
//            }
            System.arraycopy(data, 0, data, index + 1, size - index);
            data[index] = value;
            size++;
        }

        public int del(int index) {
            if (index < 0 || index >= size) {
                throw new IllegalArgumentException("index is illegal");
            }
            int delValue = data[index];
//            for (int i = index; i < size - 1; i++){
//                data[i] = data[i + 1];
//            }
            System.arraycopy(data, index + 1, data, index, size - index - 1);
            size--;
            return delValue;
        }

        public void expansion() {
            int newData[] = new int[capacity * 2];
            for (int i = 0; i < size; i++) {
                newData[i] = data[i];
            }
            data = newData;
            capacity = capacity * 2;
        }

        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append(String.format("size:%d,capacity:%d\n", size, capacity));
            sb.append("[");
            for (int i = 0; i < size; i++) {
                sb.append(data[i]);
                if (i != size - 1) {
                    sb.append(",");
                }
            }
            sb.append("]");
            return sb.toString();
        }

        public int get(int index) {
            if (index < 0 || index >= size) {
                throw new IllegalArgumentException("index is illegal");
            }
            return data[index];
        }

        public void set(int index, int value) {
            if (index < 0 || index >= size) {
                throw new IllegalArgumentException("index is illegal");
            }
            data[index] = value;
        }

        //        public void forEach(Consumer<Integer> consumer){ // 遍历,函数式接口
//            for (int datum : data) {
//                consumer.accept(datum);
//            }
//        }
        // 迭代器遍历
        @Override
        public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                int index = 0;

                @Override
                public boolean hasNext() {
                    if (index >= size) {
                        return false;
                    }
                    return true;
                }

                @Override
                public Integer next() {
                    return data[index++];
                }
            };
        }

        // stream
        public IntStream stream() {
            return IntStream.of(Arrays.copyOfRange(data, 0, size));
        }

    }
}
